package cst.a931xmc.yuejian20.Model;


import cst.a931xmc.yuejian20.Bean.RegisterBean;
import cst.a931xmc.yuejian20.Bean.UserBean;
import cst.a931xmc.yuejian20.Interface.UserIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserModel<T> extends ApiModel implements UserIface {
    private Retrofit retrofit;
    private UserService service;

    public UserModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(UserService.class);

    }

    @Override
    public void getUserDetail(int user_id, final Listener l) {
        Call<UserBean> call = service.getUserDetail(user_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void updateUserInfo(int user_id, String user_name, String user_password, String user_tel,
                                String user_sex, String user_province, String user_city, String user_intro, final Listener l){
        Call<RegisterBean> call = service.updateUserInfo(user_id,user_name,user_password,user_tel,user_sex,user_province,user_city,user_intro);
        call.enqueue(new Callback<RegisterBean>() {
            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
